
(in-package #:smugmug)

(defmethod login-with-password (email password api-key)
  (let ((response (rest-request :method "smugmug.login.withPassword"
                                :parameters `(("EmailAddress" . ,email)
                                              ("Password" . ,password)
                                              ("APIKey" . ,api-key)))))
    (let ((login-element (elt (dom:get-elements-by-tag-name response "Login") 0)))
      (make-instance 'session
                     :session-id (dom:get-attribute (elt (dom:get-elements-by-tag-name response "Session") 0) "id")
                     :user-id (dom:get-attribute (elt (dom:get-elements-by-tag-name response "User") 0) "id")
                     :account-type (dom:get-attribute login-element "AccountType")
                     :password-hash (dom:get-attribute login-element "PasswordHash")))))

(defmethod login-with-hash (user-id password-hash api-key)
  (let ((response (rest-request :method "smugmug.login.withHash"
                                :parameters `(("UserID" . ,user-id)
                                              ("PasswordHash" . ,password-hash)
                                              ("APIKey" . ,api-key)))))
    (let ((login-element (elt (dom:get-elements-by-tag-name response "Login") 0)))
      (make-instance 'session
                     :session-id (dom:get-attribute (elt (dom:get-elements-by-tag-name response "Session") 0) "id")
                     :user-id user-id
                     :account-type (dom:get-attribute login-element "AccountType")
                     :password-hash password-hash))))

(defmethod login-anonymously (api-key)
  (let ((response (rest-request :method "smugmug.login.anonymously"
                                :parameters `(("APIKey" . ,api-key)))))
    (make-instance 'session
                   :session-id (dom:get-attribute (elt (dom:get-elements-by-tag-name response "Session") 0) "id"))))
